package com.fc.redap.service.impl;

import com.fc.redap.dao.*;
import com.fc.redap.entity.*;
import com.fc.redap.exception.ResponseException;
import com.fc.redap.helper.DateTimeHelper;
import com.fc.redap.service.IProjectBookedUserService;
import com.fc.redap.service.IProjectScenicZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("projectBookedUserService")
public class ProjectBookedUserServiceImpl extends BaseServiceImpl<ProjectBookedUserEntity> implements IProjectBookedUserService {
    @Autowired
    private ProjectBookedUserDao projectBookedUserDao;

    @Autowired
    private ProjectBookedUserZoneDao projectBookedUserZoneDao;

    @Autowired
    private ProjectDao projectDao;

    @Override
    public BaseDao getDao()
    {
        return projectBookedUserDao;
    }

    @Override
    public void deleteByProjectId(Long projectId) {
        projectBookedUserDao.deleteByProjectId(projectId);
    }

    @Override
    public void save(ProjectBookedUserEntity bookedUserEntity) throws ResponseException {
        ProjectEntity projectEntity = projectDao.queryObject(bookedUserEntity.getProjectId());
        Date startOfDay = DateTimeHelper.getStartOfDay(DateTimeHelper.getCurrentDay());
        if(projectEntity.getProjectStartTime() != null
                && projectEntity.getProjectStartTime().after(startOfDay))
        {
            throw new ResponseException("旅游项目暂未开放预定!");
        }
        if(projectEntity.getProjectEndTime() != null
                && projectEntity.getProjectEndTime().before(startOfDay))
        {
            throw new ResponseException("旅游项目活动已结束!");
        }
        // 保存预定信息
        projectBookedUserDao.save(bookedUserEntity);


        // 更新场馆信息
        if(projectEntity.getType() == 1){
            //删除用户选择的场馆
            projectBookedUserZoneDao.deleteByBookId(bookedUserEntity.getId());
            //保存用户选择的场馆
            for(ProjectBookedUserZoneEntity zone: bookedUserEntity.getZoneList()){
                zone.setBookId(bookedUserEntity.getId());
                projectBookedUserZoneDao.save(zone);
            }
        }

        // 更新旅游项目的预定人数
        Map<String, Object> params = new HashMap<>();
        params.put("id", bookedUserEntity.getProjectId());
        params.put("bookedNumber", bookedUserEntity.getBookedNumber());
        params.put("updateTime", DateTimeHelper.getCurrentDay());
        projectDao.increaseBookedNumber(params);
    }

    @Override
    public List<ProjectBookedUserEntity> queryList(Map<String, Object> map) {
        List<ProjectBookedUserEntity> users = projectBookedUserDao.queryList(map);
        queryUserBookedZone(users);
        return users;
    }

    @Override
    public List<ProjectBookedUserEntity> queryHistoryList(Map<String, Object> map) {
        List<ProjectBookedUserEntity> users = projectBookedUserDao.queryHistoryList(map);
        queryUserBookedZone(users);
        return users;
    }

    private void queryUserBookedZone(List<ProjectBookedUserEntity> users) {
        List<Long> bookIds = new ArrayList<Long>();
        for(ProjectBookedUserEntity user: users){
            bookIds.add(user.getId());
        }
        List<ProjectBookedUserZoneEntity> allZoneList = projectBookedUserZoneDao.queryListByBookIds(bookIds);
        for(ProjectBookedUserEntity user: users){
            List<ProjectBookedUserZoneEntity> zoneList = new ArrayList<>();
            for(ProjectBookedUserZoneEntity zone: allZoneList){
                if(zone.getBookId() == user.getId()){
                    zoneList.add(zone);
                }
            }
            user.setZoneList(zoneList);
        }
    }

    @Override
    public List<ProjectBookedUserEntity> queryBookeUserList(Map<String, Object> map)
    {
        return projectBookedUserDao.queryList(map);
    }

    @Override
    public BookedUserStatisticEntity statisticData(Map<String, Object> map){
        List<ProjectBookedUserEntity> users = projectBookedUserDao.queryHistoryList(map);
        BookedUserStatisticEntity stat = new BookedUserStatisticEntity();
        stat.setBookedNumber(0);
        stat.setNewBookedNumber(0);
        stat.setTeamNumber(0);
        stat.setNewTeamNumber(0);
        if(users !=null && users.size()>0){
            stat.setProjectId(users.get(0).getProjectId());
            stat.setProjectName(users.get(0).getProjectName());
        }

        for(ProjectBookedUserEntity user: users){
            stat.setBookedNumber(stat.getBookedNumber()+1);
            stat.setTeamNumber(stat.getTeamNumber() + user.getBookedNumber());
            if(user.getBookedTime().after(DateTimeHelper.getCurrentDay())){
                stat.setNewBookedNumber(stat.getNewBookedNumber()+1);
                stat.setNewTeamNumber(stat.getNewTeamNumber() + user.getBookedNumber());
            }
        }
        return stat;
    }

    @Override
    public Integer getUntouchedUserNumber()
    {
        return projectBookedUserDao.getUntouchedNum();
    }
}
